
data_domain_small <- readRDS("./users_domains_feb.rds")
mediasources_mpas_insularity <- readRDS("./mediasources_mpas_insularity.rds")

# select the final 634 news sources
data_domain_small <- subset(data_domain_small, domain %in% mediasources_mpas_insularity$domain)
length(unique(data_domain_small$domain))

#### network ####
g <- graph.data.frame(data_domain_small,directed = T)
E(g)$weight <- 1 
g <- simplify(g, edge.attr.comb=list(weight="sum")) 
E(g)$weight_n <- E(g)$weight/max(E(g)$weight) 

V(g)$outdegree <- degree(g,mode = "out")
V(g)$indegree <- degree(g,mode = "in")
V(g)$degree <- degree(g,mode = "all")

media <- data.frame(name=V(g)$name,indegree=V(g)$indegree)
media <- media[with(media, order(-indegree)),]
media <- media[media$indegree > 0,]

users <- data.frame(name=V(g)$name,outdegree=V(g)$outdegree)
users <- users[with(users, order(-outdegree)),]
users <- users[users$outdegree > 0,]

V(g)$type[V(g)$indegree > 0] <- TRUE
V(g)$type[V(g)$indegree == 0] <- FALSE

g_domain <- g

write.graph(g,"./g.graphml",format = "graphml")

# bipartite projection of all graph

net <- induced.subgraph(g_domain,V(g_domain))
bipartite.projection.size(net)
g_b <- bipartite_projection(net)
write.graph(g_b["proj2"]$proj2,"./g_b.graphml",format = "graphml")

